package com.hdyg.sixg.activity.home;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.DigitalRecordAdapter;
import com.hdyg.sixg.entry.DigitalRecordBean;
import com.hdyg.sixg.mvp.Contrant.CDigitalRecord;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PDigitalRecord;
import com.hdyg.sixg.ui.CustomLoadMoreView;
import com.hdyg.sixg.util.DataCenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 数字钱包记录
 * @time 2019/4/4 16:49
 */
public class DigitalRecordActivity extends BaseActivity implements CDigitalRecord.IVDigitalRecord {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private DigitalRecordAdapter mAdapter;
    private int allDataSize, page = 1;
    private CDigitalRecord.IPDigitalRecord mPresenter;
    private List<DigitalRecordBean.ListBean> beanList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recyclerview;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.main_number_wallet);
        rvMain.setLayoutManager(new LinearLayoutManager(mContext));
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            page = 1;
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.DIGITAL_RECORD_URL, GetParamUtil.getDigitalRecordParam(page));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PDigitalRecord(this);
    }

    private void initAdapter() {
        mAdapter = new DigitalRecordAdapter(R.layout.item_recharge_record, beanList);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        rvMain.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(() -> {
            if (mAdapter.getData().size() < allDataSize) {
                page++;
                initData();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, rvMain);
    }

    @Override
    public void vGetInitSuccess(DigitalRecordBean dataBean) {
        beanList = dataBean.getList();
        allDataSize = dataBean.getTotal();
        if (page == 1) {
            if (mAdapter != null) {
                mAdapter.setNewData(beanList);
            } else {
                initAdapter();
            }
        } else {
            if (beanList.size() != 0) {
                mAdapter.addData(beanList);
            }
            mAdapter.loadMoreComplete();
        }
    }
}
